
import 'package:drive/drive.dart';
import 'package:flutter/services.dart';
import 'package:flutter_mvc_example/common/res.dart';
import 'package:flutter_mvc_example/common/widget/button.dart';
import 'package:flutter_mvc_example/mvc/good/bean/good_bean.dart';
import 'package:flutter_mvc_example/mvc/good/bean/shop_bean.dart';

import 'controller.dart';
import 'package:flutter/material.dart';
import 'package:flutter_mvc/flutter_mvc.dart';
import 'widget.dart';

///description:商品pop
class GoodValuePopPage extends BasePage<GoodPopController> {
  List<GoodDataPropFormatBean> get propFormat =>
      c.data.propFormat;

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Container(
        height: 492.dp + MediaQuery.of(context).padding.bottom,
        decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: resBorderRadius(topLeft: 8.dp, topRight: 8.dp)),
        child: Column(
          children: [
            Container(
              height: 44.dp,
              child: Stack(
                children: [
                  Center(
                    child: Text(
                      "商品属性",
                      style: resTextStyle30.copyWith(
                          fontSize: 16.sp, fontWeight: FontWeight.w600),
                    ),
                  ),
                  Positioned(
                      right: 12.dp,
                      top: 0,
                      bottom: 0,
                      child: GestureDetector(
                        onTap: () {
                          Navigator.of(context).pop();
                        },
                        child: Container(
                          alignment: Alignment.centerLeft,
                          child: Image.asset(
                            "images/yp_shangpin_xq_gm_gb.png",
                            width: 20.dp,
                            height: 20.dp,
                          ),
                        ),
                      ))
                ],
              ),
            ),
            Expanded(
                child: ListView.builder(
              physics: BouncingScrollPhysics(),
              padding: EdgeInsets.only(),
              itemBuilder: (ctx, index) {
                final item = propFormat[index];
                return _buildItem(item.name, item.value);
              },
              itemCount: propFormat.length,
            )),
            _buildBottomItem()
          ],
        ),
      ),
    );
  }

  Widget _buildItem(String title, String value) {
    return Container(
      height: 45.dp,
      child: Column(
        children: [
          Container(
            height: 43.dp,
            child: Row(
              children: [
                Container(
                  width: 88.dp,
                  padding: EdgeInsets.only(left: 12.dp),
                  alignment: Alignment.centerLeft,
                  child: Text(
                    title,
                    maxLines: 1,
                    overflow: TextOverflow.ellipsis,
                    style: resTextStyle7A.copyWith(fontSize: 12.sp),
                  ),
                ),
                Expanded(
                    child: Text(
                  value,
                  style: resTextStyle30.copyWith(fontSize: 12.sp),
                ))
              ],
            ),
          ),
          Container(
            color: Color(0xffE2E2E2),
            height: 1.dp,
            margin: EdgeInsets.only(left: 12.dp, right: 12.dp),
          )
        ],
      ),
    );
  }

  Widget _buildBottomItem() {
    return GestureDetector(
      onTap: () {
        Navigator.of(context).pop();
      },
      child: Container(
          color: Colors.white,
          child: SafeArea(
              top: false,
              child: Container(
                height: 50.dp,
                padding: EdgeInsets.only(left: 12.dp, right: 12.dp),
                alignment: Alignment.centerLeft,
                child: DMainButton(
                  "完成",
                  null,
                  36.dp,
                  borderRadius: 8.dp,
                ),
              ))),
    );
  }
}

class GoodSkuPopPage extends BasePage<GoodPopController> {
  @override
  Widget build(BuildContext context) {
    return KeyboardSpace(
      child: Container(
        alignment: Alignment.bottomRight,
        child: Container(
          height: 492.dp + MediaQuery.of(context).padding.bottom,
          child: Stack(
            children: [
              Positioned.fill(
                  child: Container(
                decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius:
                        resBorderRadius(topRight: 14.dp, topLeft: 14.dp)),
                margin: EdgeInsets.only(top: 12.dp),
              )),
              Positioned.fill(
                  child: Column(
                children: [
                  SkuTitleItem(),
                  Container(
                    height: 1.dp,
                    color: Color(0xfff2f2f2),
                  ),
                  Spec1Item(),
                  Container(
                    height: 1.dp,
                    color: Color(0xfff2f2f2),
                  ),
                  Expanded(
                      child: ListView.builder(
                    padding: EdgeInsets.only(),
                    physics: BouncingScrollPhysics(),
                    itemBuilder: (ctx, index) {
                      final item = c.selectSpec.child[index];
                      return SkuItem(item, c);
                    },
                    itemCount: c.selectSpec.child.length,
                  )),
                  Container(
                    height: 1.dp,
                    color: Color(0xfff2f2f2),
                  ),
                  StatisticsItem(),
                  Container(
                    height: 1.dp,
                    color: Color(0xfff2f2f2),
                  ),
                  c.type==GoodPopController.car?_buildBottomItem():
                  Container(
                    height: 50.dp,
                    padding: EdgeInsets.only(left: 12.dp,right: 12.dp),
                    child: Row(
                      children: [
                        _buildButton(
                            "加入购物车",
                            [
                              Color(0xffF87A0B),
                              Color(0xffF28B24),
                            ],
                            resBorderRadius(bottomLeft: 8.dp, topLeft: 8.dp),
                            c.onAddCarClick),
                        _buildButton(
                            "立即购买",
                            [
                              Color(0xffDE271D),
                              Color(0xffFEF2B21),
                            ],
                            resBorderRadius(topRight: 8.dp, bottomRight: 8.dp),
                            c.onPayClick),
                      ],
                    ),
                  )
                ],
              ))
            ],
          ),
        ),
      ),
    );
  }

  Widget _buildButton(String text, List<Color> colors,
      BorderRadius borderRadius, Function onTap) {
    return Expanded(
        child: GestureDetector(
      onTap: onTap,
      child: Container(
        height: 36.dp,
        child: Center(
          child: Text(
            text,
            style: resTextStyle.copyWith(color: Colors.white),
          ),
        ),
        decoration: BoxDecoration(
            borderRadius: borderRadius,
            gradient: LinearGradient(
                colors: colors,
                begin: Alignment.topCenter,
                end: Alignment.bottomCenter)),
      ),
    ));
  }

  Widget _buildBottomItem() {
    return GestureDetector(
      onTap:c.onSuccessClick,
      child: Container(
          color: Colors.white,
          child: SafeArea(
              top: false,
              child: Container(
                height: 50.dp,
                padding: EdgeInsets.only(left: 12.dp, right: 12.dp),
                alignment: Alignment.centerLeft,
                child: DMainButton(
                  "完成",
                  null,
                  36.dp,
                  borderRadius: 8.dp,
                ),
              ))),
    );
  }
}

class GoodContactPopPage extends BasePage<GoodPopContactController> {
  ShopDataContactBean get contact => c.shopData.contact;

  @override
  Widget build(BuildContext context) {
    return Container(
      alignment: Alignment.bottomCenter,
      child: Container(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            Container(
              margin: EdgeInsets.only(left: 8.dp, right: 8.dp),
              decoration: BoxDecoration(
                  color: Color(0xffEDEDED),
                  borderRadius: BorderRadius.circular(14.dp)),
              child: Column(
                mainAxisSize: MainAxisSize.min,
                children: [
                  Container(
                    height: 45.dp,
                    child: Center(
                      child: Text(
                        "卖家联系方式",
                        style: resTextStyle7A.copyWith(fontSize: 13.sp),
                      ),
                    ),
                  ),
                  for (var phone in contact.phone)
                    _buildItem("images/yp_shangpin_xq_lx_dh.png", phone, () {
                      onPhoneClick(phone);
                    }),
                  for (var qq in contact.qq)
                    _buildItem("images/yp_shangpin_xq_lx_qq.png", qq, () {
                      onCopyClick(qq);
                    }),
                  for (var wx in contact.wx)
                    _buildItem("images/yp_shangpin_xq_lx_wx.png", wx, () {
                      onCopyClick(wx);
                    }),
                ],
              ),
            ),
            SizedBox(
              height: 8.dp,
            ),
            SafeArea(
              top: false,
              child: GestureDetector(
                onTap: () {
                  Navigator.of(context).pop();
                },
                child: Container(
                  height: 56.dp,
                  decoration: BoxDecoration(
                      color: Colors.white,
                      borderRadius: BorderRadius.circular(14.dp)),
                  margin:
                      EdgeInsets.only(left: 8.dp, right: 8.dp, bottom: 14.dp),
                  child: Center(
                    child: Text(
                      "取消",
                      style: resTextStyle.copyWith(
                          color: Color(0xff417CF2), fontSize: 20.sp),
                    ),
                  ),
                ),
              ),
            )
          ],
        ),
      ),
    );
  }

  void onPhoneClick(String phone) {
    ToastUtils.show("拨打电话");
    Navigator.of(context).pop();
  }

  void onCopyClick(String value) {
    Clipboard.setData(ClipboardData(text: value));
    Navigator.of(context).pop();
    ToastUtils.show("复制成功");
  }

  Widget _buildItem(String asset, String num, Function onTap) {
    return GestureDetector(
      onTap: onTap,
      child: Container(
        height: 56.dp,
        color: Color(0x01ffffff),
        child: Column(
          children: [
            Container(
              height: 1.dp,
              color: Color(0xff111111).withOpacity(0.2),
            ),
            Expanded(
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Icon(
                    Icons.add_to_home_screen,
                    size:24.dp
                  ),
                  SizedBox(
                    width: 16.dp,
                  ),
                  Text(
                    num,
                    style: resTextStyle30.copyWith(fontSize: 20.sp),
                  )
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}
